package usf.gwt.ui.bootstrap.client;

import usf.gwt.ui.bootstrap.client.core.BootstrapContainer;
import usf.gwt.ui.bootstrap.client.core.JqueryUtils;

import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Text;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;

public class NavItem extends BootstrapContainer implements HasClickHandlers {
	
	public NavItem() {}
	
    public NavItem(String text) {
    	setText(text);
	}
    public NavItem(IconTypes icon) {
    	setIcon(icon);
	}
    public NavItem(IconTypes icon, String text) {
    	setIcon(icon);
    	setText(text);
	}
	
	@Override
	protected Element initWidget() {
		Element e = Document.get().createLIElement();
		AnchorElement a = Document.get().createAnchorElement();
		a.setHref("javascript:;");
		Text text = Document.get().createTextNode("");
		a.appendChild(text);
		e.appendChild(a);
		return e;
	}
	
	public String getText() {
		return getTextElement().getNodeValue();
	}
	public void setText(String text) {
		getTextElement().setNodeValue(text);
	}
    public void setIcon(IconTypes icon) {
    	getBaseElement().setClassName(icon.getFullValue());
    }
    public IconTypes getIcon() {
		return IconTypes.valueOf(getBaseElement().getClassName());
	}
	
	public boolean isActive() {
		return JqueryUtils.isActive(getElement());
	}
	public void setActive(boolean value) {
		JqueryUtils.activate(getElement(), value);
	}
	public void setActiveUnique() {
		JqueryUtils.activateUnique(getElement(), true);
	}
	public boolean isEnabled() {
		return JqueryUtils.isStyleEnabled(getElement());
	}	
	public void setEnable(boolean enable) {
		JqueryUtils.setStyleEnabled(getElement(), enable);
	}
	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return addDomHandler(handler, ClickEvent.getType());
	}
	
	@Override
	protected AnchorElement getBaseElement() {
		return getElement().getFirstChildElement().cast();
	}
	protected Text getTextElement() {
		return getBaseElement().getFirstChild().cast();
	}
}
